package com.example.carbon.server.info;

import com.alibaba.fastjson2.JSON;
import com.example.carbon.dao.MaterialProductDao;
import com.example.carbon.server.MaterialProductServerInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MaterialProductServer implements MaterialProductServerInfo {
    @Resource
    MaterialProductDao materialProductDao;

    /***
     * 进行序列化的进行插入数据
     * @param modelId
     * @param materialIdList
     * @return
     */
    @Override
    public boolean insertNewInfo(String modelId, List<String> materialIdList) {
        try {
            materialProductDao.deleteRelInfoByModelId(modelId);
            for (String materialId : materialIdList) {
                materialProductDao.insertNewInfo(modelId, materialId);
            }
            return true;
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }

    }

    /**
     * 删除关系操作,根据模型来删除
     *
     * @param modelId
     * @return
     */
    @Override
    public boolean deleteInfoByModelId(String modelId) {
        try {
            return materialProductDao.deleteRelInfoByModelId(modelId);
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }
    }

    /**
     * 删除关系操作，通过物料档案删除
     * 在进行物料一些操作的时候，要进行一些操作
     *
     * @param materialId
     * @return
     */
    @Override
    public boolean deleteInfoByMaterialId(String materialId) {
        try {
            return materialProductDao.deleteRelInfoByMaterialId(materialId);
        } catch (Exception e) {
            log.info(e.getMessage());
            return false;
        }
    }

    /**
     * 通过模型的id来获取相对应的物料档案的信息
     *
     * @param modelId
     * @return
     */
    @Override
    public String selectMaterialByModelId(String modelId) {
        try {
            return JSON.toJSONString(materialProductDao.selectByModelId(modelId));
        } catch (Exception e) {
            log.info(e.getMessage());
            return null;
        }
    }


}
